﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JoyfulHomeJourney.BackEnd.Infrastructure.Atrributes;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Requests;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Responses;
using JoyfulHomeJourney.BackEnd.Infrastructure.Tools;
using JoyfulHomeJourney.BackEnd.Server.EFCore;
using JoyfulHomeJourney.BackEnd.Server.Interfaces;
using Microsoft.EntityFrameworkCore;

namespace JoyfulHomeJourney.BackEnd.Server.Instances
{
    internal class UploadEF
    {
        public UploadEF(DbContext dbContext)
        {
            sysCarSuppliesServerEF = new SysCarSuppliesServerEF(dbContext);
            sysCarServicesServerEF = new SysCarServicesServerEF(dbContext);
            sysProductsServerEF = new SysProductsServerEF(dbContext);
        }
        public SysCarSuppliesServerEF sysCarSuppliesServerEF { get; set; }
        public SysCarServicesServerEF sysCarServicesServerEF { get; set; }
        public SysProductsServerEF sysProductsServerEF { get; set; }
    }
    [InjectClass]
    public class UploadServer : IUploadServer
    {
        private readonly UploadEF _uploadEF;

        public UploadServer(DbContext dbContext)
        {
            _uploadEF = new UploadEF(dbContext);
        }

        public async Task<ResponseApiDto> UploadProductImage(RequestUploadDto dto)
        {
            if (dto.file == null || dto.id == null)
            {
                return ResponseApiDto.Fail("缺少必要字段");
            }
            var product = await _uploadEF.sysProductsServerEF.Query(d => d.PId == dto.id).SingleOrDefaultAsync();
            if (product == null)
            {
                return ResponseApiDto.Fail("产品不存在");
            }
            if (!string.IsNullOrEmpty(product.PImages) && product.PImages.Split(",").Length + 1 > 10)
            {
                return ResponseApiDto.Fail("最多上传十张图片");
            }
            int affectedRows = 0;
            string fileName = "";
            await _uploadEF.sysCarSuppliesServerEF.ExecuteInTransactionAsync(async () =>
            {
                // 上传图片
                switch (product.PCategory)
                {
                    case "sys_car_supplies":
                        fileName = await Tools.UploadImage(dto.file,Path.Combine(Tools.GetImageCarSuppliesPath(),dto.id));
                        break;
                    case "car_services":
                        fileName = await Tools.UploadImage(dto.file, Path.Combine(Tools.GetImageCarServicePath(), dto.id));
                        break;
                    default:
                        throw new Exception("商品必须具有类别");
                }
                // 更新对图片的引用
                if (string.IsNullOrEmpty(product.PImages))
                {
                    product.PImages = fileName;
                }
                else
                {
                    product.PImages += "," + fileName;
                }
                affectedRows += await _uploadEF.sysProductsServerEF.UpdateAllProperAsync(product);
            }, () =>
            {
                affectedRows = 0;
                // 删除文件
                Tools.DeleteImage(Path.Combine(Tools.GetImageCarSuppliesPath(), dto.id, fileName));
                return null;
            });
            return ResponseApiDto.Create(affectedRows);
        }
    }
}
